/*
 * Split Ensembl Dsn Source Id
 */
YAHOO.dasBrowser.genome.karyotypeStructure.dsn.splitEnsemblDsnSourceId = function(sourceId){
	var sId = new Object();
	var sourceIdArray_ = sourceId.split(".");
    if (sourceIdArray_.length > 3) {
		sId.sourceSpecie = sourceIdArray_[0];
		sId.sourceRelease = sourceIdArray_[1];
		sId.sourceType = sourceIdArray_[sourceIdArray_.length - 1];
		for (var j = 2; j < sourceIdArray_.length - 1; j++) {
			sId.sourceRelease = sId.sourceRelease + "." + sourceIdArray_[j];
		}
	 } else {
        sId.sourceSpecie = sourceIdArray_[0];
        sId.sourceRelease = sourceIdArray_[1];
        sId.sourceType = sourceIdArray_[2];
    }
	return sId;
}


/*
 * Create chromosome Objects from Entry Points information
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.createChromosomeObjects = function(selectedChromosomeNames){
	//console.log(YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.ids);
	//var entryPointsIds = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.ids;
	//for (var j = 0; j < entryPointsIds.length; j++) {	
	for (var j = 0; j < selectedChromosomeNames.length; j++) {
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosomeNames[j]] = new myChromosome();
		if(YAHOO.lang.isNull(YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.karyotypeLocation)){
			YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosomeNames[j]].url = "";
		} else {
			var karyotypeSourceUrl = YAHOO.dasBrowser.genome.karyotypeStructure.dsn.allDsn[YAHOO.dasBrowser.genome.karyotypeStructure.dsn.selectedDsn.karyotypeLocation].sourceUrl;
			var chromosomeId = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosomeNames[j]].id;
			var chromosomeUrl = karyotypeSourceUrl + "/features?segment=" + chromosomeId // Example: http://www.ensembl.org/das/Homo_sapiens.NCBI36.karyotype/features?segment=1
			YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosomeNames[j]].url = chromosomeUrl;
		}
			
		//YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomes[selectedChromosomeNames[j]].url = "test/ensemblChr22Bands.xml"; // For testing purposes
		var chromosomeLength = YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.allEntryPoints[selectedChromosomeNames[j]].length;
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.mappingChrIdChrLength[selectedChromosomeNames[j]] = parseInt(chromosomeLength);
		//YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.mappingChrIdEntryPointsSegmentId[selectedChromosomeNames[j]] = chromosomeId;
		//YAHOO.dasBrowser.util.printOnTest(chromosomeUrl);
		//YAHOO.dasBrowser.genome.karyotypeStructure.proxyUrl
	}
	return true;
}

/*
 * Sort slected Entry Points
 */
YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.sortSelectedEntryPoints = function(selectedEntryPoints, selectedType){
	var arrayTextValues = new Array();
	var arrayNumberValues = new Array();
	var newArray = new Array();
	var newArrayPrefix = new Array();
	for (var j = 0; j < selectedEntryPoints.length; j++) {
		var entryPoint = selectedEntryPoints[j].split(selectedType);
		var entryPointId = entryPoint[1];
		var isNum = Number(entryPointId);
		if (!isNaN(isNum)) {
			arrayNumberValues.push(parseInt(entryPointId));
		}
		else {
			arrayTextValues.push(entryPointId);
		}
	}
	// Improve sorting
	
	//for (var j = 0; j < arrayNumberValues.length; j++) {
	//	console.log(arrayNumberValues[j]);
	//}
	arrayNumberValues = arrayNumberValues.sort(function(a,b){return a - b});
	//console.log(arrayNumberValues.length);
	arrayTextValues = arrayTextValues.sort(function(a,b){return a.length - b.length});
	//console.log(arrayTextValues.length);
	newArray = arrayNumberValues.concat(arrayTextValues);
	//console.log(newArray.length);
	
	// Improve atach prefix
	for (var j = 0; j < newArray.length; j++) {
		newArrayPrefix.push(selectedType + newArray[j]);
	}
	//console.log(selectedEntryPoints);
	//console.log(newArrayPrefix);
	YAHOO.dasBrowser.genome.karyotypeStructure.entryPoints.selectedEntryPoints.ids = newArrayPrefix;
	return true;

}	
	


YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.stylesheet.setStylesheetValuesAndColors = function(allTypes){
	var stylesheet = new Array();
	for (var j = 0; j < allTypes.length; j++) {
		stylesheet[allTypes[j].typeId] = allTypes[j].bgcolor;
	}
	return stylesheet;
};


YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.stylesheet.setCustomValuesAndColors = function(){
	localStylesheet = new Array();
	//localStylesheet["band:"] = "#FDFFB4"; //yellow
	localStylesheet["band:"] = "#CCCCCC";
	localStylesheet["band:tel"] = "#708090";
	localStylesheet["band:tip"] = "#708090"; // slategrey // slategrey
	localStylesheet["band:stalk"] = "#708090"; // slategrey // slategrey
	localStylesheet["band:acen"] = "#708090"; // slategrey // slategrey
	localStylesheet["default"] = "#FF0000"; // red // red
	localStylesheet["band:mark"] = "#0000FF"; // blue // blue
	localStylesheet["band:gpos"] = "#000000"; // black // black
	localStylesheet["band:gpos100"] = "#000000"; // black // black
	localStylesheet["band:gpos75"] = "#666666"; // grey40 // grey40
	localStylesheet["band:gpos66"] = "#666666";
	localStylesheet["band:gpos50"] = "#999999"; // grey60 // grey60
	localStylesheet["band:gpos33"] = "#CCCCCC";
	localStylesheet["band:gpos25"] = "#CCCCCC"; // grey80 // grey85
	localStylesheet["band:gvar"] = "#CCCCCC"; // grey80 // grey88
	localStylesheet["band:gneg"] = "#f2f2f2"; // grey90 // white
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.stylesheet.valuesAndColors = localStylesheet;
	return true;
}
/*
 * Calculate Px Width for chromosomes in the karyotype 
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.setMappingChrIdChrWidhtPx = function(mappingChrIdChrLength,chromosomeProportion){
	for(var i in mappingChrIdChrLength){
		chomosomeWidthPx = mappingChrIdChrLength[i] * YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.chromosomeProportion;
		YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.mappingChrIdChrWidhtPx[i] = parseInt(chomosomeWidthPx);
	}
}



/*
 * Get number of Label Lines for a chromosome taking into account the number of pixels of the chromosome
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.getNumberOfChromosomeLabelLines = function(chromosomeLabelLinesPerPx, chromosomeWidthPx){
	var numberOfLines = 0;
	for(x in chromosomeLabelLinesPerPx){
		if(chromosomeWidthPx >= parseInt(x)){
			numberOfLines = chromosomeLabelLinesPerPx[x];
		}
	}
	return numberOfLines;
}

/*
 * Calculate and set the chromosome width for the karyotype view
 */
YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.calculateKaryotypeChromosomeWidthPx = function(){
	var chrWidth = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeConf.chromosomeWidthPx;
	var karyotypeColumns = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.karyotypeColumns;
	var karyotypeColumnsMargin = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.karyotypeColumnsMargin;
	var chromosomeLeftPadding = YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.chromosomeLeftPaddingPx
	var newChrWidth = (chrWidth/karyotypeColumns)- karyotypeColumnsMargin - chromosomeLeftPadding;
	YAHOO.dasBrowser.genome.karyotypeStructure.karyotype.karyotypeConf.chromosomeWidthPx = newChrWidth;
}	